package com.up.upzl.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.up.upzl.biz.domain.SysRole;
import com.up.upzl.biz.service.SysRoleService;

/**
 * 系统用户控制器
 * 
 * @author geeks
 *
 */
@Controller
@RequestMapping("sysRole")
public class SysRoleController {

	@Autowired
	private SysRoleService sysRoleService;

	@GetMapping("list")
	public String RoleList(String roleName, Pageable page, Model model) {
		Page<SysRole> list = sysRoleService.findByCondition(roleName, page);
		model.addAttribute("pageList", list);
		return "sysRole/list";
	}
	/**
	 * 跳转到添加页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("addPage")
	public String add(Model model) {
		List<SysRole> roles = sysRoleService.findAll();
		model.addAttribute("roles", roles);
		return "sysRole/add";
	}

	/**
	 * 保存对象
	 */
	@PostMapping("add")
	public @ResponseBody SysRole save(SysRole sysRole) {
		return sysRoleService.save(sysRole);
	}

	/**
	 * 跳转到更新页面
	 */
	@GetMapping({"updatePage","detail"})
	public String update(Long id, Model model) {
		SysRole sysRole = sysRoleService.findOne(id);
		model.addAttribute("entity", sysRole);
		List<SysRole> roles = sysRoleService.findAll();
		model.addAttribute("roles", roles);
		return "sysRole/update";
	}

	/**
	 * 更新
	 */
	@PostMapping("update")
	public @ResponseBody SysRole update(SysRole sysRole) {
		Assert.notNull(sysRole.getId(), "Id can`t be null");
		return sysRoleService.save(sysRole);
	}
	
	/**
	 * 删除
	 */
	@PostMapping("delete")
	public @ResponseBody Map<String, String> delete(SysRole sysRole) {
		Assert.notNull(sysRole.getId(), "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			sysRoleService.delete(sysRole.getId());
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}
	
	
}
